package com.glory.gloryquickui.view


import android.content.Context
import android.view.View
import android.widget.TextView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.starbar.StarBar

/**
 *星星评分组件
 */
class RattingBarController(context: Context?, val viewModel: QuickUIViewModel) :
    ViewController<FieldBean?>(context) {

    private var starBar: StarBar? = null
    private var tvRattingName: TextView? = null
    private var tvRattingValue: TextView? = null

    override fun resLayoutId(): Int {
        return R.layout.layout_ratting_bar
    }

    override fun onBindView(data: FieldBean?) {
        tvRattingName!!.text = data!!.config.label
        starBar!!.setIntegerMark(!data.allowHalf)
        starBar!!.setStartCount(data.max)

//        starBar!!.setIsClick(data.config.required)
        starBar!!.setIsClick(!data.disabled)

        starBar!!.starMark = data.config.defaultValue.toString().toFloat()
        tvRattingValue!!.text = data.config.defaultValue.toString()
        tvRattingValue!!.visibility = if (data.showScore) View.VISIBLE else View.GONE
        starBar!!.setOnStarChangeListener {
            tvRattingValue!!.text = it.toString()
            data.config.defaultValue = it
            val map = viewModel.pageData.value!!.toMutableMap()
            map[data!!.vModel] = it.toString()
            viewModel.pageData.value = map
        }
    }

    override fun onCreatedView(view: View?) {
        starBar = view?.findViewById(R.id.starBar)
        tvRattingName = view?.findViewById(R.id.tvRattingName)
        tvRattingValue = view?.findViewById(R.id.tvRattingValue)
    }

    override fun masterPart(): View? {
        return starBar
    }
}